﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Client.Models {

    public class ApplicationModel {

        #region Private Properties

        private Application application = null;

        private String pageTitle = "Physis";


        private Exception lastException = null;

        private String informationMessage = String.Empty;

        #endregion


        #region Public Properties

        public Application Application { 

            get {

                if (application == null) {
                    
                    // RETREIVE PREVIOUS APPLICATION OBJECT FROM SESSION STORAGE  

                    application = (Application)System.Web.HttpContext.Current.Session["Physis.Core.Application"];

                    if (application == null) {

                        // CREATE NEW, SAVE TO LOCAL AND SESSION STATE BY SETTER 

                        Application = new Application ();

                    }

                }

                return application; 

            }

            set {

                application = value; // SAVE LOCAL 
                
                System.Web.HttpContext.Current.Session["Physis.Core.Application"] = value; // SAVE INTO SESSION 
            
            }                            

        }

        public String PageTitle { get { return pageTitle; } set { pageTitle = value; } }


        public Boolean HasException { get { return (lastException != null); } }

        public String ExceptionMessage { get { return ((lastException != null) ? lastException.Message : String.Empty); } }

        public Boolean HasInformationMessage { get { return (!String.IsNullOrWhiteSpace (informationMessage)); } }

        public String InformationMessage { get { return informationMessage; } } 

        #endregion 


        #region Constructors

        public ApplicationModel () { /* DO NOTHING */ }

        /// <summary>
        /// Constructor that allows the base Application Model to extract the 
        /// saved properties from the inline Page State (where written in the view).
        /// </summary>
        /// <param name="form">This should be the Form property of the Request.</param>
        public ApplicationModel (System.Collections.Specialized.NameValueCollection form) : this () {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public virtual void UpdateValues (System.Collections.Specialized.NameValueCollection form) { /* DO NOTHING */ }

        public void SetException (Exception exception) { 

            lastException = exception;

            return;

        }

        public void SetException (String message) {

            lastException = new Exception (message); 

            return;

        }

        public void SetInformationMessage (String forInformationMessage) {

            informationMessage = forInformationMessage;

            return;

        }

        #endregion

    }

}
